{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 正则表达式介绍\n",
    "\n",
    "## 什么是正则表达式？\n",
    "[**Regex**](https://zh.wikipedia.org/wiki/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F)（英语：Regular Expression，在代码中常简写为 regex 、 regexp 或 RE ），又称正规表示式、正規表示法、正規運算式、規則運算式、常規表示法，是计算机科学的一个概念 ，正则表达式是一种编写匹配字符串的模式的方法。通常这些模式可用于搜索特定事物的字符串，或搜索然后替换某些事物等。正则表达式非常适合字符串操作！\n",
    "\n",
    "## 为什么正则表达式很重要？\n",
    "从本指南的第一段开始，您可能已经猜到了，但 **每当您必须处理字符串** 时正则表达式非常有用。从源码中一组类似命名变量的基本重命名到 [数据预处理](https://github.com/clone95/Virgilio/blob/master/zh-CN/Specializations/HardSkills/DataPreprocessing.md) 。正则表达式通常提供简洁的方式来表达您想要查找的任何类型的事物。例如，如果你想解析一个表格并寻找某人可能出生的年份，你可以使用类似 `(19) |(20) [0-9][0-9]` 的东西。这是正则表达式的一个例子！\n",
    "\n",
    "## 先决条件\n",
    "\n",
    "本指南不假设任何先验知识。示例将使用 Python 编码，但既不假设也不需要掌握编程语言。欢迎您在浏览器中阅读该指南或下载该指南并运行示例/使用它们进行操作。\n",
    "\n",
    "# 目录\n",
    "- [基本正则表达式](#基本正则表达式)\n",
    "    - [使用 Python re](#使用-Python-re)\n",
    "    - [查找 $\\pi$](#查找-$\\pi$)\n",
    "- [匹配选项](#匹配选项)\n",
    "    - [Virgilio 还是 Virgil？](#Virgilio-还是-Virgil？)\n",
    "- [匹配重复](#匹配重复)\n",
    "    - [贪婪](#贪婪)\n",
    "    - [删除多余的空格](#删除多余的空格)\n",
    "- [字符组](#字符组)\n",
    "    - [电话号码 v1](#电话号码-v1)\n",
    "- [更多 `re` 函数](#更多-re-函数)\n",
    "    - [`search` 结合 `match`](#search-结合-match)\n",
    "    - [`findall` 匹配计数](#findall-匹配计数)\n",
    "- [特殊字符](#特殊字符)\n",
    "    - [电话号码 v2](#电话号码-v2)\n",
    "- [分组](#分组)\n",
    "    - [电话号码 v3](#电话号码-v3)\n",
    "- [正则表达式的玩具项目](#正则表达式的玩具项目)\n",
    "- [进一步阅读](#进一步阅读)\n",
    "- [推荐解决方案](#推荐解决方案)\n",
    " \n",
    "让我们一起深入！"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**只是一句简单的话：**每当我向你展示新的东西时，我都试图加入一些小练习，这样你就可以尝试测试你的知识。解决方案的示例在[notebook 的末尾]中提供（#推荐解决方案）。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 基本正则表达式\n",
    "\n",
    "正则表达式只是以特定格式编写的字符串，然后可以由特定工具 / 库 / 程序用于对字符串执行模式匹配。在本指南中，我们将使用 `这种格式` 来引用正则表达式！\n",
    "\n",
    "可以创建的最简单的正则表达式只由常规字符组成。如果你想在文本中找到所有出现的单词 _\"Virgilio\"_ ，你可以编写正则表达式`Virgilio`。在这个正则表达式中，没有角色做任何特殊或不同的事情。实际上，这个正则表达式只是一个普通的单词。没关系，毕竟正则表达式是字符串！\n",
    "\n",
    "如果给你的文字_\\\"Project Virgilio is great\\\"_，你可以使用你的 `Virgilio` 正则表达式找到单词 _\\\"Virgilio\\\"_ 的出现。但是，如果文本是__\\\"Project virgilio is great\\\"_，那么你的正则表达式将不起作用，因为正则表达式默认情况下是**区分大小写**，因此应该完全匹配所有内容。我们说 `Virgilio` 字面上符合字符序列 \\\"Virgilio\\\"。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 使用 Python re\n",
    "\n",
    "要检查我们的正则表达式是否运行良好并让您有机会直接进行实验，我们将使用 Python 的 `re` 模块来处理正则表达式。要使用 `re` 模块，我们首先导入它，然后定义一个正则表达式，然后在字符串上使用 `search()` 函数！真简单："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'Virgilio' is in 'Project Virgilio is great'\n",
      "'Virgilio' is not in 'Project virgilio is great'\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "regex = \"Virgilio\"\n",
    "str1 = \"Project Virgilio is great\"\n",
    "str2 = \"Project virgilio is great\"\n",
    "\n",
    "if re.search(regex, str1):\n",
    "    print(\"'{}' is in '{}'\".format(regex, str1))\n",
    "else:\n",
    "    print(\"'{}' is not in '{}'\".format(regex, str1))\n",
    "    \n",
    "if re.search(regex, str2):\n",
    "    print(\"'{}' is in '{}'\".format(regex, str2))\n",
    "else:\n",
    "    print(\"'{}' is not in '{}'\".format(regex, str2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`re.search(regex，string)` 函数将正则表达式作为第一个参数，然后搜索作为第二个参数给出的字符串上的任何匹配项。但是，函数的返回值是 **不是** 一个布尔值，而是一个 *匹配对象* ："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<re.Match object; span=(8, 16), match='Virgilio'>\n"
     ]
    }
   ],
   "source": [
    "print(re.search(regex, str1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "匹配对象具有关于遇到的匹配的相关信息：开始和结束位置，匹配的字符串，甚至是更复杂的正则表达式的一些其他内容。\n",
    "\n",
    "我们可以看到，在这种情况下匹配与正则表达式完全相同，因此看起来匹配对象内部的 `match` 信息是无关紧要的……但是只要我们将选项或重复引入到我们的正则表达式。\n",
    "\n",
    "如果没有找到匹配项，则 `.search()` 函数返回 `None` ："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "print(re.search(regex, str2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "每当匹配不是 `None` 时，我们可以保存返回的匹配对象并使用它来提取所有需要的信息！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The match started at pos 8 and ended at pos 16\n",
      "Or with tuple notation, the match is at (8, 16)\n",
      "And btw, the actual string matched was 'Virgilio'\n"
     ]
    }
   ],
   "source": [
    "m = re.search(regex, str1)\n",
    "if m is not None:\n",
    "    print(\"The match started at pos {} and ended at pos {}\".format(m.start(), m.end()))\n",
    "    print(\"Or with tuple notation, the match is at {}\".format(m.span()))\n",
    "    print(\"And btw, the actual string matched was '{}'\".format(m.group()))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "现在你应该尝试你自己的文字正则表达式来匹配更多和处理匹配失败。我提供了三个我自己的例子："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The match is at (20, 25)\n",
      "\n",
      "Woops, did I just got the alphabet wrong..?\n",
      "\n",
      "I just matched 'a' inside 'aaaaa aaaaaa a aaa'\n"
     ]
    }
   ],
   "source": [
    "m1 = re.search(\"regex\", \"This guide is about regexes\")\n",
    "if m1 is not None:\n",
    "    print(\"The match is at {}\\n\".format(m1.span()))\n",
    "\n",
    "m2 = re.search(\"abc\", \"The alphabet goes 'abdefghij...'\")\n",
    "if m2 is None:\n",
    "    print(\"Woops, did I just got the alphabet wrong..?\\n\")\n",
    "    \n",
    "s = \"aaaaa aaaaaa a aaa\"\n",
    "m3 = re.search(\"a\", s)\n",
    "if m3 is not None:\n",
    "    print(\"I just matched '{}' inside '{}'\".format(m3.group(), s))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 查找 $\\pi$\n",
    "\n",
    "$$\\pi = 3.1415\\cdots$$\n",
    "\n",
    "对吗？好吧，点之后会发生什么？一个无限的数字序列，对吗？可能是您的出生日期出现在 $\\pi$ 的前一百万位数？好吧，我们可以使用正则表达式来找出它！更改下面的 `regex` 变量，以 $\\pi$ 的前百万位数字查找您的出生日期或您想要的任何数字！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "pifile = \"regex-bin/pi.txt\"\n",
    "regex = \"\"  # define your regex to look your favourite number up\n",
    "\n",
    "with open(pifile, \"r\") as f:\n",
    "    pistr = f.read()  # pistr is a string that contains 1M digits of pi\n",
    "    \n",
    "## search for your number here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "要搜索 $\\pi$ 的前 1 亿位数字(或 2 亿，我没有真正使用它)你可以查看 [这个](https://www.angio.net/pi/piquery) 网站。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 匹配选项\n",
    "\n",
    "我们刚看到一个非常简单的正则表达式试图在文本中找到 _\"Virgilio\"_ 这个词，但我们也发现我们没有灵活性，甚至无法处理有人可能忘记将名称大写的事实正确地拼写它像 _\"virgilio\"_ 。\n",
    "\n",
    "为了防止这样的问题，可以以处理不同可能性的方式编写正则表达式。对于我们的情况，我们希望第一个字母是 _\"V\"_ 或 _\"v\"_ ，然后应该是 _\"irgilio\"_ 。\n",
    "\n",
    "为了处理不同的可能性，我们使用字符 `|` 。例如， `V | v` 与字母 vee 匹配，无论其大小写如何："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "small v found\n",
      "big V found\n"
     ]
    }
   ],
   "source": [
    "v = \"v\"\n",
    "V = \"V\"\n",
    "regex = \"v|V\"\n",
    "if re.search(regex, v):\n",
    "    print(\"small v found\")\n",
    "if re.search(regex, V):\n",
    "    print(\"big V found\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "现在我们可以连接第一个字母的正则表达式和 `irgilio` 正则表达式(对于名称的其余部分)来获得与Virgilio名称匹配的正则表达式，无论其第一个字母的大小写如何："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "virgilio found!\n",
      "Virgilio found!\n"
     ]
    }
   ],
   "source": [
    "virgilio = \"virgilio\"\n",
    "Virgilio = \"Virgilio\"\n",
    "regex = \"(V|v)irgilio\"\n",
    "if re.search(regex, virgilio):\n",
    "    print(\"virgilio found!\")\n",
    "if re.search(regex, Virgilio):\n",
    "    print(\"Virgilio found!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请注意，我们用括号编写正则表达式：`(V|v)irgilio`\n",
    "\n",
    "如果我们只写 `V|virgilio` ，那么正则表达式将匹配 \"V\" 或 \"virgilio\" ，而不是 \"Virgilio\" 或 \"virgilio\" ："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<re.Match object; span=(29, 30), match='V'>\n"
     ]
    }
   ],
   "source": [
    "regex = \"V|virgilio\"\n",
    "print(re.search(regex, \"This sentence only has a big V\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "所以我们真的需要在那里用括号括起来 `(V|v)` 。如果我们这样做，它将按预期工作！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<re.Match object; span=(27, 35), match='virgilio'>\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "regex = \"(V|v)irgilio\"\n",
    "print(re.search(regex, \"The name of the project is virgilio, but with a big V!\"))\n",
    "print(re.search(regex, \"This sentence only has a big V\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "也许你甚至没有注意到，但还有其他事情发生了！请注意，我们使用了 `|`, `(` 和 `)` ，并且这些不存在于单词 _\"virgilio\"_ 中，但是我们的正则表达式 `(V|v)irgilio` 匹配它......是因为这三个字符在正则表达式世界中具有特殊含义，因此 **不是** 字面上的解释，与 `irgilio` 中的任何字母发生的情况相反。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Virgilio 还是 Virgil？\n",
    "\n",
    "以下是维基百科[关于维吉尔的文章](https://en.wikipedia.org/wiki/Virgil)的几段话：\n",
    "\n",
    " > Publius Vergilius Maro(古典拉丁语：[puː.blɪ.ʊswɛrɡɪ.lɪ.ʊssa.roː];传统日期公元前70年10月15日 - 公元前19年9月21日[1])，通常称为Virgil或Vergil(/vɜːrdʒɪl /)在英语中，是古代罗马诗人的奥古斯都时期。他写了三首最着名的拉丁文学诗：Eclogues(或Bucolics)，Georgics和史诗Aeneid。附录Vergiliana收集的一些小诗有时归于他。[2] [3]\n",
    "\n",
    ">维吉尔传统上被评为罗马最伟大的诗人之一。他的埃涅伊德自成立以来一直被认为是古罗马的民族史诗。以荷马的伊利亚特和奥德赛为蓝本，埃涅伊德追随特洛伊难民埃涅阿斯，因为他努力实现自己的命运并到达意大利，在那里他的后代罗穆卢斯和雷木思将建立罗马城。维吉尔的作品对西方文学产生了广泛而深远的影响，尤其是但丁的神曲，其中维吉尔作为通过地狱和炼狱的丹特指南出现。\n",
    " \n",
    "\"Virgilio\"是意大利形式的\"Virgil\"，我编辑了上面的段落以获得意大利语版本而不是英语版本。我要你还原吧！\n",
    "\n",
    "你可能想看看Python中的[`while`周期](https://realpython.com/python-while-loop/)，[string indexing](https://www.digitalocean.com/community/教程/ how-to-index-and-slice-strings-in-python-3)和[string concatenation](https://realpython.com/python-string-split-concatenate-join/)。关键是你找到了一个匹配，你将字符串分解为_before_匹配部分和_after_匹配部分，然后将这两个与_Virgilio_粘合在一起。\n",
    "\n",
    "请注意，[字符串替换](https://www.tutorialspoint.com/python/string_replace.htm)可能会更快更容易，但这会破坏本练习的目的。修复所有内容后，打印最终结果以确保您修复了每次出现的名称。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "paragraphs = \\\n",
    "\"\"\"Publius Vergilius Maro (Classical Latin: [ˈpuː.blɪ.ʊs wɛrˈɡɪ.lɪ.ʊs ˈma.roː]; traditional dates October 15, 70 BC – September 21, 19 BC[1]), usually called virgilio or Vergil (/ˈvɜːrdʒɪl/) in English, was an ancient Roman poet of the Augustan period. He wrote three of the most famous poems in Latin literature: the Eclogues (or Bucolics), the Georgics, and the epic Aeneid. A number of minor poems, collected in the Appendix Vergiliana, are sometimes attributed to him.[2][3]\n",
    "\n",
    "Virgilio is traditionally ranked as one of Rome's greatest poets. His Aeneid has been considered the national epic of ancient Rome since the time of its composition. Modeled after Homer's Iliad and Odyssey, the Aeneid follows the Trojan refugee Aeneas as he struggles to fulfill his destiny and reach Italy, where his descendants Romulus and Remus were to found the city of Rome. virgilio's work has had wide and deep influence on Western literature, most notably Dante's Divine Comedy, in which virgilio appears as Dante's guide through Hell and Purgatory.\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 匹配重复\n",
    "\n",
    "有时我们想要找到具有可重复位的模式。例如，当人们看到像婴儿一样可爱的东西时，人们会发出 _\"awww\"_ 或 _\"owww\"_ 声音。但我在那里使用 _\"w\"_ 的数量完全是武断的！如果宝宝真的很可爱，有人可能会写 _\"awwwwwwwwwww\"_ 。那么我怎么能写一个匹配 _\"aww\"_ 和 _\"oww\"_ 的正则表达式，但是有任意数量的字符 _\"w\"_ ？\n",
    "\n",
    "我将通过针对以下字符串测试正则表达式来说明捕获重复的几种方法：\n",
    "\n",
    " - \"awww\" (3 letters \"w\")\n",
    " - \"awwww\" (4 letters \"w\")\n",
    " - \"awwwwwww\" (7 letters \"w\")\n",
    " - \"awwwwwwwwwwwwwwww\" (16 letters \"w\")\n",
    " - \"aw\" (1 letter \"w\")\n",
    " - \"a\" (0 letters \"w\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "cute_strings = [\n",
    "    \"awww\",\n",
    "    \"awwww\",\n",
    "    \"awwwwwww\",\n",
    "    \"awwwwwwwwwwwwwwww\",\n",
    "    \"aw\",\n",
    "    \"a\"\n",
    "]\n",
    "\n",
    "def match_cute_strings(regex):\n",
    "    \"\"\"Takes a regex, prints matches and non-matches\"\"\"\n",
    "    for s in cute_strings:\n",
    "        m = re.search(regex, s)\n",
    "        if m:\n",
    "            print(\"match: {}\".format(s))\n",
    "        else:\n",
    "            print(\"non match: {}\".format(s))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 至少一次\n",
    "\n",
    "如果我想匹配所有包含 **至少** 一个 \"w\" 的字符串，我们可以使用字符 `+` 。一个 `+` 意味着我们想要找到 **左边的任何一个或多个重复** 。例如，正则表达式 \"a+\" 将匹配任何至少有一个 \"a\" 的字符串。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "match: awww\n",
      "match: awwww\n",
      "match: awwwwwww\n",
      "match: awwwwwwwwwwwwwwww\n",
      "match: aw\n",
      "non match: a\n"
     ]
    }
   ],
   "source": [
    "regex = \"aw+\"\n",
    "match_cute_strings(regex)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 任意次数\n",
    "\n",
    "如果我想匹配包含任意数量字母 \"w\" 的所有字符串，我可以使用字符 `*` 。字符 \"*\" 表示 **匹配任意数量的重复** ，无论其左边是什么，_甚至0次重复_！因此，正则表达式 \"a*\" 将匹配空字符串 \"\"，因为空字符串 \"\" 具有 0 个字母 \"a\" 的重复。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "match: awww\n",
      "match: awwww\n",
      "match: awwwwwww\n",
      "match: awwwwwwwwwwwwwwww\n",
      "match: aw\n",
      "match: a\n"
     ]
    }
   ],
   "source": [
    "regex = \"aw*\"\n",
    "match_cute_strings(regex)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 特定次数\n",
    "\n",
    "如果我想匹配包含特定粒子的字符串特定次数，我可以使用 `{n}` 表示法，其中 `n` 被我想要的重复次数所取代。例如， `a{3}` 匹配字符串 \"aaa\" 但不匹配字符串 \"aa\" 。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "match: awww\n",
      "match: awwww\n",
      "match: awwwwwww\n",
      "match: awwwwwwwwwwwwwwww\n",
      "non match: aw\n",
      "non match: a\n"
     ]
    }
   ],
   "source": [
    "regex = \"aw{3}\"\n",
    "match_cute_strings(regex)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**等一下**，为什么模式 `aw{3}` 匹配更长的可爱表达，比如 \"awwww\" 或 \"awwwwwww\" ？因为正则表达式试图找到与模式匹配的_子串_。我们的模式是 `awww` (如果我明确地写了 `w{3}` 并且字符串 **awww**w 有那个子字符串，就像字符串 **awww**wwww 有它，或者更长的版本 16 个字母 \"w\" 。如果我们想要排除字符串 \"awwww\" ， \"awwwwwww\" 和 \"awwwwwwwwwwwwww\" ，我们将不得不修复我们的正则表达式。一个更好的例子来说明 `{n}` 的工作方式是考虑 \"wow\", \"woow\" 和 \"wooooooooooooow\" 娱乐表达，而不是表达可爱。我们定义了一些娱乐表达方式：\n",
    "\n",
    " - \"wow\"\n",
    " - \"woow\"\n",
    " - \"wooow\"\n",
    " - \"woooow\"\n",
    " - \"wooooooooow\"\n",
    " \n",
    "现在我们测试我们的`{3}`模式。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "wow_strings = [\n",
    "    \"wow\",\n",
    "    \"woow\",\n",
    "    \"wooow\",\n",
    "    \"woooow\",\n",
    "    \"wooooooooow\"\n",
    "]\n",
    "\n",
    "def match_wow_strings(regex):\n",
    "    \"\"\"Takes a regex, prints matches and non-matches\"\"\"\n",
    "    for s in wow_strings:\n",
    "        m = re.search(regex, s)\n",
    "        if m:\n",
    "            print(\"match: {}\".format(s))\n",
    "        else:\n",
    "            print(\"non match: {}\".format(s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "non match: wow\n",
      "non match: woow\n",
      "match: wooow\n",
      "non match: woooow\n",
      "non match: wooooooooow\n"
     ]
    }
   ],
   "source": [
    "regex = \"wo{3}w\"\n",
    "match_wow_strings(regex)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 介于 $n$ 和 $m$ 之间\n",
    "\n",
    "只用三个 \"o\" 来表达娱乐是可以的，但是人们也可以使用两个或四个 \"o\" 。我们如何捕获可变数量的字母，但是在一定范围内？假设我只想捕获 2 到 4 个字母 \"o\" 之间的 \"哇\" 版本。我可以用 `{2,4}` 做到这一点。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "non match: wow\n",
      "match: woow\n",
      "match: wooow\n",
      "match: woooow\n",
      "non match: wooooooooow\n"
     ]
    }
   ],
   "source": [
    "regex = \"wo{2,4}w\"\n",
    "match_wow_strings(regex)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 最高 $n$ 次或至少 $m$ 次\n",
    "\n",
    "现在我们正在玩我们可能想要的重复类型，但当然我们可能会说我们想要 **不超过** $n$ 重复，你可以用 `{，n}` 实现或者我们做想要 **至少** $m$ 重复，你可以用 `{m，}` 做到。\n",
    "\n",
    "实际上，看看这些正则表达式："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "match: wow\n",
      "match: woow\n",
      "match: wooow\n",
      "match: woooow\n",
      "non match: wooooooooow\n"
     ]
    }
   ],
   "source": [
    "regex = \"wo{,4}w\" # should not match strings with more than 4 o's\n",
    "match_wow_strings(regex)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "non match: wow\n",
      "non match: woow\n",
      "match: wooow\n",
      "match: woooow\n",
      "match: wooooooooow\n"
     ]
    }
   ],
   "source": [
    "regex = \"wo{3,}w\" # should not match strings with less than 3 o's\n",
    "match_wow_strings(regex)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 生存还是毁灭\n",
    "\n",
    "最后但同样重要的是，有时我们会关心可能存在或可能不存在的事物。例如，上面我们用英语和意大利语版本的 Virgilio 进行了处理。如果我们想编写一个正则表达式来捕获两个版本，我们可以编写 `((V|v)irgil)|((V|v)irgilio)` ，或稍微更紧凑， `(V|v)((irgil)|(irgilio))` 。但这看起来并不好，对吧？我们需要说的是，最终的 \"io\" 可能存在也可能不存在。我们用 `?` 字符做这个。所以正则表达式 `(V|v)irgil(io)?` 匹配 \"Virgil\" 和 \"Virgilio\" 的大小写版本。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The name virgil was matched!\n",
      "The name Virgil was matched!\n",
      "The name virgilio was matched!\n",
      "The name Virgilio was matched!\n"
     ]
    }
   ],
   "source": [
    "regex = \"(V|v)irgil(io)?\"\n",
    "names = [\"virgil\", \"Virgil\", \"virgilio\", \"Virgilio\"]\n",
    "for name in names:\n",
    "    m = re.search(regex, name)\n",
    "    if m:\n",
    "        print(\"The name {} was matched!\".format(name))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 贪婪\n",
    "\n",
    "`+`，`？`，`*`和`{，}`运算符都是贪婪的。这是什么意思？这意味着他们会尽可能地匹配。它们具有此默认行为，而不是在满足正则表达式时停止尝试查找更多匹配项。为了更好地说明我的意思，让我们再看一下我们一直处理的 `match` 对象中包含的信息："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<re.Match object; span=(0, 3), match='aaa'>\n"
     ]
    }
   ],
   "source": [
    "regex = \"a+\"\n",
    "s = \"aaa\"\n",
    "m = re.search(regex, s)\n",
    "print(m)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "注意打印信息中写着 `match='aaa'` 的部分。函数 `m.group()` 会让我知道正则表达式匹配的实际字符串是什么，在这种情况下它是 \"aaa\" 。好吧，我写的正则表达式，`a+`，将匹配一或多个字母 \"a\" 。如果我在字符串上使用正则表达式并得到匹配，如果我无法访问该类型的信息，我怎么能知道匹配了多少 \"a\" ？如果我无法访问该类型的信息呢？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "aaa\n"
     ]
    }
   ],
   "source": [
    "print(m.group())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "因此，让我们验证一下，事实上，我提到的操作都是贪婪的。同样，因为它们都匹配尽可能多的角色。\n",
    "\n",
    "下面，我们看到给出一个 30 个字母 \"a\" 的字符串，\n",
    "\n",
    "   - 模式 `a?` 匹配 1 个 \"a\"，这是尽可能多的\n",
    "   - 模式 `a+` 匹配 30 个 \"a\"，这是尽可能多的\n",
    "   - 模式 `a*` 也匹配 30 个\n",
    "   - 模式 `a{5,10}` 匹配10 个 \"a\"，这是我们施加的限制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n",
      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n",
      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n",
      "aaaaaaaaaa\n"
     ]
    }
   ],
   "source": [
    "s = \"a\"*30\n",
    "print(re.search(\"a?\", s).group())\n",
    "print(re.search(\"a+\", s).group())\n",
    "print(re.search(\"a*\", s).group())\n",
    "print(re.search(\"a{5,10}\", s).group())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果我们不希望我们的操作员贪婪，我们只需在它们之后添加一个 `?` 。所以下面的正则表达式 **不是** 贪婪的：\n",
    "\n",
    "    - 模式 `a??` 将 **不** 匹配字符，很像 `a*?` ，因为现在他们的目标是尽可能少地匹配。但是长度为 0 的匹配是最短的匹配！\n",
    "    - 模式 `a+?` 只匹配 1 个 \"a\"\n",
    "    - 模式 `a{5,10}?` 只匹配 5 个 \"a\"\n",
    "  \n",
    "我们可以通过运行下面的代码轻松确认我刚才所说的内容。请注意，现在我以不同的方式打印东西，因为否则我们将无法看到 `a??` 和 `a*?` 模式没有匹配。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "''\n",
      "'a'\n",
      "''\n",
      "'aaaaa'\n"
     ]
    }
   ],
   "source": [
    "s = \"a\"*30\n",
    "print(\"'{}'\".format(re.search(\"a??\", s).group()))\n",
    "print(\"'{}'\".format(re.search(\"a+?\", s).group()))\n",
    "print(\"'{}'\".format(re.search(\"a*?\", s).group()))\n",
    "print(\"'{}'\".format(re.search(\"a{5,10}?\", s).group()))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 删除多余的空格\n",
    "\n",
    "现在我们知道了重复，我将告诉你关于 `sub` 函数的信息，我们将使用它来解析一段文本并删除所有存在的额外空格。输入 `re.sub(regex，rep，string)` 将在给定的字符串上使用给定的正则表达式，并且无论何时匹配，它都会删除匹配并将 `rep` 放在那里。\n",
    "\n",
    "例如，我可以使用它来替换所有英文/意大利名称 Virgilio 的标准版本："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Virgilio has many names, like Virgilio, Virgilio, Virgilio, Virgilio, or even Virgilio.\n"
     ]
    }
   ],
   "source": [
    "s = \"Virgilio has many names, like virgil, virgilio, Virgil, Vergil, or even vergil.\"\n",
    "regex = \"(V|v)(e|i)rgil(io)?\"\n",
    "\n",
    "print(\n",
    "    re.sub(regex, \"Virgilio\", s)\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "现在轮到你了。我将把这句话作为输入，你的工作是修复其中的空白。完成后，将结果保存在名为 `s` 的字符串中，并检查 `s.count(\"\")` 是否等于0。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "weird_text = \"Now   it  is your   turn.  I am     going  to give   you this    sentence as        input, and   your  job    is to      fix the     whitespace         in it. When you    are  done,    save the    result in a  string  named   `s`, and   check    if  `s.count(\"  \")` is   equal   to    0  or not.\"\n",
    "regex = \"\"  # put your regex here\n",
    "\n",
    "# substitute the extra whitespace here\n",
    "# save the result in 's'\n",
    "\n",
    "# this print should be 0\n",
    "print(s.count(\"  \"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 字符组\n",
    "\n",
    "到目前为止，我们一直在使用编写一些简单的正则表达式来匹配某些单词，一些名称以及类似的东西。现在我们有一个不同的计划。我们将编写一个与美国电话号码匹配的正则表达式，我们假设它们的格式为 xxx-xxx-xxxx 。前三位数是区号，但我们不关心区号是否真正有意义。那我们怎么匹配呢？\n",
    "\n",
    "事实上，我怎样才能匹配第一个数字？它可以是0到9之间的任何数字，所以我应该写 `(0|1|2|3|4|5|6|7|8|9)` 以匹配第一个数字，然后重复？实际上，我们可以做到这一点，是的，获得这个正则表达式：\n",
    "\n",
    "`(0|1|2|3|4|5|6|7|8|9){3}-(0|1|2|3|4|5|6|7|8|9){3}-(0|1|2|3|4|5|6|7|8|9){4}`\n",
    "\n",
    "这有用吗？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<re.Match object; span=(0, 12), match='202-555-0181'>\n",
      "None\n",
      "None\n",
      "<re.Match object; span=(0, 12), match='512-555-0191'>\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "regex = \"(0|1|2|3|4|5|6|7|8|9){3}-(0|1|2|3|4|5|6|7|8|9){3}-(0|1|2|3|4|5|6|7|8|9){4}\"\n",
    "numbers = [\n",
    "    \"202-555-0181\",\n",
    "    \"202555-0181\",\n",
    "    \"202 555 0181\",\n",
    "    \"512-555-0191\",\n",
    "    \"96-125-3546\",\n",
    "]\n",
    "for nr in numbers:\n",
    "    print(re.search(regex, nr))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "它看起来很有效，但肯定有更好的方法......而且有！我们实际上可以编写一系列值，而不是像我们一样写出每一个数字！事实上，正则表达式 `[0-9]` 匹配从 0 到 9 的所有数字。所以我们实际上可以将我们的正则表达式缩短为 `[0123456789]{3}-[0123456789]{3}-[0123456789]{4}` ："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<re.Match object; span=(0, 12), match='202-555-0181'>\n",
      "None\n",
      "None\n",
      "<re.Match object; span=(0, 12), match='512-555-0191'>\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "regex = \"[0-9]{3}-[0-9]{3}-[0-9]{4}\"\n",
    "numbers = [\n",
    "    \"202-555-0181\",\n",
    "    \"202555-0181\",\n",
    "    \"202 555 0181\",\n",
    "    \"512-555-0191\",\n",
    "    \"96-125-3546\",\n",
    "]\n",
    "for nr in numbers:\n",
    "    print(re.search(regex, nr))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里的魔力是由 `[]` 来完成的，它表示一个字符组。 `[]` 的工作方式是，正则表达式会尝试匹配内部的任何内容，而恰好是 \"0-9\" 是列出所有数字的较短方式。当然你也可以匹配 `[0123456789]{3}-[0123456789]{3}-[0123456789]{4}` 这比我们的第一次尝试略短，但仍然非常糟糕。类似于 `0-9`，我们有 `a-z` 和 `A-Z` ，它们遍历字母表中的所有字母。\n",
    "\n",
    "您也可以在不同的地方开始和结束，例如 `c-o` 可用于匹配仅使用 \"c\" 和 \"o\"之 间的字母的单词，如 \"hello\" ："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<re.Match object; span=(0, 5), match='hello'>\n",
      "<re.Match object; span=(1, 4), match='ice'>\n"
     ]
    }
   ],
   "source": [
    "regex = \"[c-o]+\"\n",
    "print(re.search(regex, \"hello\"))\n",
    "print(re.search(regex, \"rice\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用这些字符组，我们实际上可以将我们的 Virgilio 正则表达式重写为略短的东西，从 `(V|v)(e|i)rgil(io)?` 到 `[Vv][ie]rgil(io)?`。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Virgilio has many names, like Virgilio, Virgilio, Virgilio, Virgilio, or even Virgilio.\n"
     ]
    }
   ],
   "source": [
    "s = \"Virgilio has many names, like virgil, virgilio, Virgil, Vergil, or even vergil.\"\n",
    "regex = \"[Vv][ie]rgil(io)?\"\n",
    "\n",
    "print(\n",
    "    re.sub(regex, \"Virgilio\", s)\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们再一次看到我们的正则表达式与 **ice** 中的 r**ice** 匹配，因为 \"r\" 不在合法的字母范围内，但 **ice** 是。\n",
    "\n",
    "_字符组_ 是方括号 `[]` ，无论里面是什么。另外，请注意我们使用的特殊字符在字符组中失去了意义！所以 `[()？+ * {}]` 实际上会匹配任何这些字符："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<re.Match object; span=(25, 26), match='?'>\n"
     ]
    }
   ],
   "source": [
    "regex = \"[()?+*{}]\"\n",
    "print(re.search(regex, \"Did I just ask a question?\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "关于字符组的最后一点，如果它们以 `^` 开头，那么我们实际上是在说 \"使用除了里面的内容以外的一切\"："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n",
      "<re.Match object; span=(0, 1), match='r'>\n"
     ]
    }
   ],
   "source": [
    "regex = \"[^c-o]+\"\n",
    "print(re.search(regex, \"hello\"))\n",
    "print(re.search(regex, \"rice\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 电话号码 v1\n",
    "\n",
    "既然您知道如何使用字符组来表示范围，那么您需要编写一个匹配美国电话号码的正则表达式，格式为 xxx-xxx-xxxx 。不仅如此，您还必须应对这样一个事实，即国家指标可能会或可能不会出现这些数字，您可以假设它看起来像 \"+1\" 或 \"001\" 。国家指示符可以用空格或短划线与数字的其余部分分开。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<re.Match object; span=(0, 0), match=''>\n",
      "<re.Match object; span=(0, 0), match=''>\n",
      "<re.Match object; span=(0, 0), match=''>\n",
      "<re.Match object; span=(0, 0), match=''>\n",
      "<re.Match object; span=(0, 0), match=''>\n",
      "<re.Match object; span=(0, 0), match=''>\n"
     ]
    }
   ],
   "source": [
    "regex = \"\"  # write your regex here\n",
    "matches = [  # you should be able to match those\n",
    "    \"202-555-0181\",\n",
    "    \"001 202-555-0181\",\n",
    "    \"+1-512-555-0191\"\n",
    "]\n",
    "non_matches = [  # for now, none of these should be matched\n",
    "    \"202555-0181\",\n",
    "    \"96-125-3546\",\n",
    "    \"(+1)5125550191\"\n",
    "]\n",
    "for s in matches:\n",
    "    print(re.search(regex, s))\n",
    "for s in non_matches:\n",
    "    print(re.search(regex, s))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 更多 `re` 函数\n",
    "\n",
    "到目前为止，我们只查看了 `re` 模块的 `.search()` 函数，但是现在我将告诉你一些在处理模式匹配时非常方便的函数。当你完成这个小部分时，你现在将使用以下函数： `match()` ， `search()` ， `findall()` ， `sub()` 和 `split()` 。\n",
    "\n",
    "如果你在这里主要用于正则表达式，并且你不太关心在 Python 中使用它们，你可以浏览这一部分......即使它仍然是一个很好的阅读。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### `search()` 和 `sub()`\n",
    " \n",
    "您已经知道这两个函数，`re.search(regex，string)` 将尝试在给定的 `string` 中找到由 `regex` 给出的模式，并在 `match` 对象中返回匹配的信息。函数 `re.sub(regex，rep，string)` 将采用正则表达式和两个字符串;然后它将查找您在 `string` 中指定的模式，并将匹配替换为您给出的其他字符串 `rep` 。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### `match()`\n",
    "\n",
    "函数 `re.match(regex，string)` 类似于函数 `re.search()` ，除了 `.match()` 只会检查你的模式是否适用于字符串的 **开头** 。也就是说，如果你的字符串没有 **以你提供的模式开始** ，那么函数会返回 \"None\" 。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ".search() found abc in abcdef\n",
      ".search() found abc in the alphabet starts with abc\n",
      ".match() says that abcdef starts with abc\n"
     ]
    }
   ],
   "source": [
    "regex = \"abc\"\n",
    "string1 = \"abcdef\"\n",
    "string2 = \"the alphabet starts with abc\"\n",
    "# the .search() function finds the patterns, regardless of position\n",
    "if re.search(regex, string1):\n",
    "    print(\".search() found {} in {}\".format(regex, string1))\n",
    "if re.search(regex, string2):\n",
    "    print(\".search() found {} in {}\".format(regex, string2))\n",
    "    \n",
    "# the .match() function only checks if the string STARTS with the pattern\n",
    "if re.match(regex, string1):\n",
    "    print(\".match() says that {} starts with {}\".format(string1, regex))\n",
    "if re.match(regex, string2):  # this one should NOT print\n",
    "    print(\".match() says that {} starts with {}\".format(string2, regex))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### `findall()`\n",
    "\n",
    "`re.findall(正则表达式，字符串)` 与 `.search()` 函数完全相同，不同之处在于它将返回 **所有** 它可以找到的匹配项，而不仅仅是第一个。它不返回 `match` 对象，而只返回匹配的字符串。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<re.Match object; span=(0, 3), match='wow'>\n",
      "['wow', 'wow', 'wow']\n"
     ]
    }
   ],
   "source": [
    "regex = \"wow\"\n",
    "string = \"wow wow wow!\"\n",
    "\n",
    "print(re.search(regex, string))\n",
    "\n",
    "print(re.findall(regex, string))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<re.Match object; span=(0, 3), match='ab1'>\n",
      "['ab1', 'ab2', 'ab3']\n"
     ]
    }
   ],
   "source": [
    "regex = \"ab[0-9]\"\n",
    "string = \"ab1 ab2 ab3\"\n",
    "\n",
    "print(re.search(regex, string))\n",
    "\n",
    "print(re.findall(regex, string))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "值得注意的是， `findall()` 函数只返回 _非重叠_ 匹配。也就是说，有人可能会在 \"wowow\" 中说 `wow` 出现两次，开始时： **wow**ow ，结束时： wo**wow**。 `findall()` 只返回一个匹配，因为第二个匹配与第一个匹配有重叠："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['wow']\n"
     ]
    }
   ],
   "source": [
    "regex = \"wow\"\n",
    "string = \"wowow\"\n",
    "print(re.findall(regex, string))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "有了这些信息，现在考虑一下我们之前展示的运算符的贪婪程度会更有意义，比如 `？` 和 `+` 。想象一下，我们正在处理正则表达式 \"a +\" ，我们有一个字符串 \"aaaaaaaaa\" 。如果我们使用贪婪版本的 `+` ，那么我们得到一个匹配，这是整个字符串。如果我们使用运算符 `+` 的非贪婪版本，也许是因为我们想要尽可能多的匹配，我们将获得一堆 \"a\" 匹配！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['aaaaaaaaa']\n",
      "['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']\n"
     ]
    }
   ],
   "source": [
    "regex_greedy = \"a+\"\n",
    "regex_nongreedy = \"a+?\"\n",
    "string = \"aaaaaaaaa\"\n",
    "\n",
    "print(re.findall(regex_greedy, string))\n",
    "\n",
    "print(re.findall(regex_nongreedy, string))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### `split()`\n",
    "\n",
    "`re.split(regex，string)` 将给定的字符串拆分成位，只要它能够找到你指定的模式。假设我们有兴趣在一个句子中查找连续辅音的所有序列(我不知道为什么你会想要......)。然后我们可以使用元音和空格 \"\" 来分解句子："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Th', 's', 's', 'j', 'st', 'r', 'g', 'l', 'r', 's', 'nt', 'nc', '']\n"
     ]
    }
   ],
   "source": [
    "regex = \"[aeiou ]+\" # this will eliminate all vowels/spaces that appear consecutively\n",
    "string = \"This is just a regular sentence\"\n",
    "\n",
    "print(re.split(regex, string))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  `search` 结合 `match`\n",
    "\n",
    "回想一下`match()`函数只检查你的模式是否在字符串的开头。我想要你做的是定义你自己的`search`函数，它接受一个正则表达式和一个字符串，如果模式在字符串内，则返回 `True` ，否则返回 `False` 。你可以做到吗？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "def my_search(regex, string):\n",
    "    pass  # write your code here\n",
    "\n",
    "regex = \"[0-9]{2,4}\"\n",
    "\n",
    "# your function should be able to match in all these strings\n",
    "string1 = \"1984 was already some years ago.\"\n",
    "string2 = \"There is also a book whose title is '1984', but the story isn't set in the year of 1984.\"\n",
    "string3 = \"Sometimes people write '84 for short.\"\n",
    "\n",
    "# your function should also match with this regex and this string\n",
    "regex = \"a*\"\n",
    "string = \"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `findall` 匹配计数\n",
    "\n",
    "现在我想要你定义 `count_matches` 函数，它接受一个正则表达式和一个字符串，并返回给定字符串中存在的非重叠匹配的数量。你可以做到吗？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "def count_matches(regex, string):\n",
    "    pass  # your code goes here\n",
    "\n",
    "regex = \"wow\"\n",
    "\n",
    "string1 = \"wow wow wow\" # this should be 3\n",
    "string2 = \"wowow\" # this should be 1\n",
    "string3 = \"wowowow\" # this should be 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 特殊字符\n",
    "\n",
    "是时候提升一点了！我们已经看到一些具有特殊意义的角色，现在我将介绍其中的一些角色！我将从列出它们开始，然后我将更详细地解释它们：\n",
    "\n",
    "- `.` 用于匹配 **任何** 字符，换行符除外\n",
    "- `^` 用于匹配字符串的开头\n",
    "- `$` 用于匹配字符串的末尾\n",
    "- `\\d` 用于匹配任何数字\n",
    "- `\\w` 用于匹配任何字母数字字符\n",
    "- `\\s` 用于匹配任何类型的空格\n",
    "- `\\` 用于删除字符的特殊含义"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 点 ` .`\n",
    "\n",
    "可以在正则表达式中使用`.`来捕获可能在那里使用过的任何字符，只要我们仍在同一行中。也就是说，`.`不起作用的唯一地方是我们改变了文本中的行。想象一下这个模式是 `d.ck`。然后模式将匹配\n",
    "\n",
    "```\n",
    "\"duck\"\n",
    "```\n",
    "\n",
    "但它不匹配\n",
    "\n",
    "```\n",
    "\"d\n",
    "ck\"\n",
    "```\n",
    "\n",
    "因为我们改变了字符串中间的行。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 脱字符 ` ^`\n",
    "\n",
    "如果我们在正则表达式的开头使用 `^` ，那么我们只关心字符串开头的匹配。也就是说，`^wow` 只会匹配以 \"wow\" 开头的字符串："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<re.Match object; span=(0, 3), match='wow'>\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "regex = \"^wow\"\n",
    "\n",
    "print(re.search(regex, \"wow, this is awesome\"))\n",
    "print(re.search(regex, \"this is awesome, wow\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "回想一下，字符组中的 `^` 也可以表示 \"除了这个类中的任何内容之外的任何内容\" ，因此正则表达式 `[^d]uck` 将匹配任何包含 **uck** 的字符串，只要它不是 \"duck\" 这个词。如果插入符号 `^` 出现在字符组 `[]` 中但它不是第一个字符，那么它没有特殊含义，它只代表字符本身。这意味着正则表达式 `[()^{}]` 正在寻找匹配列出的任何字符："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<re.Match object; span=(0, 1), match='^'>\n",
      "<re.Match object; span=(0, 1), match='('>\n",
      "<re.Match object; span=(0, 1), match='}'>\n"
     ]
    }
   ],
   "source": [
    "regex = \"[()^{}]\"\n",
    "print(re.search(regex, \"^\"))\n",
    "print(re.search(regex, \"(\"))\n",
    "print(re.search(regex, \"}\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 美元符号 `$`\n",
    "\n",
    "与插入符号 `$` 相反，美元符号仅在字符串末尾匹配！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n",
      "<re.Match object; span=(17, 20), match='wow'>\n"
     ]
    }
   ],
   "source": [
    "regex = \"wow$\"\n",
    "\n",
    "print(re.search(regex, \"wow, this is awesome\"))\n",
    "print(re.search(regex, \"this is awesome, wow\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "将 `^` 与 `$` 结合起来意味着我们希望将整个字符串与我们的模式相匹配。例如 `^[a-zA-Z ]*$` 检查我们的字符串是否只包含字母和空格而不包含其他内容："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<re.Match object; span=(0, 47), match='this is a sentence with only letters and spaces'>\n",
      "None\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "regex = \"^[a-zA-Z ]*$\"\n",
    "\n",
    "s1 = \"this is a sentence with only letters and spaces\"\n",
    "s2 = \"this sentence has 1 number\"\n",
    "s3 = \"this one has punctuation...\"\n",
    "\n",
    "print(re.search(regex, s1))\n",
    "print(re.search(regex, s2))\n",
    "print(re.search(regex, s3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 字符组 `\\d` ，`\\w` 和 `\\s`\n",
    "\n",
    "每当你看到反斜杠后跟一个字母时，这可能意味着正在进行 _特殊_ 匹配。这三个特殊的 \"字符\" 是一些字符组 `[]` 的简写符号。例如，`\\d` 与 `[0-9]` 相同。 `\\w` 表示任何字母数字字符(如字母，数字和 `_` )，而 `\\s` 表示任何空格字符(如空格 \"\"，制表符，换行符等)。\n",
    "\n",
    "我展示的所有这三个特殊字符都可以大写。如果他们是，那么他们的意思恰恰相反！所以 `\\D` 的意思是\"**除**数字之外的任何字符\"，`\\W` 表示 \"**除** 字母数字之外的任何字符\"，而 `\\S` 表示 \"**除** 空格之外的任何字符\"。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['these are some words']\n"
     ]
    }
   ],
   "source": [
    "regex = \"\\D+\"\n",
    "s = \"these are some words\"\n",
    "print(re.findall(regex, s))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "除此之外，这些特殊字符可以在字符组中使用，例如`[abc \\ d]`将匹配任何数字和字母\"a\"，\"b\"和\"c\"。如果使用了插入符号`^`，那么我们将排除特殊字符所指的任何内容。例如，如果`[\\ d]`匹配任何数字，那么`[^ \\ d]`将匹配任何不是数字的东西。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 反斜杠 `\\`\n",
    "\n",
    "我们已经看到在字母之前使用反斜杠给它们一些特殊含义......好吧，特殊字符之前的反斜杠也剥夺了它的特殊含义！所以，如果你想匹配一个反斜杠，你可以使用 `\\\\` 。如果你想匹配我们已经看过的任何其他特殊字符，你可以在它们之前添加一个 `\\` ，比如 `\\+` 来匹配一个加号。下一个正则表达式可用于匹配添加表达式，如 \"16 + 6\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<re.Match object; span=(0, 6), match='16 + 6'>\n",
      "<re.Match object; span=(0, 6), match='4325+2'>\n",
      "<re.Match object; span=(0, 6), match='4+ 564'>\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "regex = \"[\\d]+ ?\\+ ?[\\d]+\"\n",
    "add1 = \"16 + 6\"\n",
    "add2 = \"4325+2\"\n",
    "add3 = \"4+ 564\"\n",
    "mult1 = \"56 * 2\"\n",
    "\n",
    "print(re.search(regex, add1))\n",
    "print(re.search(regex, add2))\n",
    "print(re.search(regex, add3))\n",
    "print(re.search(regex, mult1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 电话号码 v2\n",
    "\n",
    "现在我邀请您查看 [电话号码 v1](#电话号码-v1) 并重写您的正则表达式，以包含一些您之前不知道的新特殊字符！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<re.Match object; span=(0, 0), match=''>\n",
      "<re.Match object; span=(0, 0), match=''>\n",
      "<re.Match object; span=(0, 0), match=''>\n",
      "<re.Match object; span=(0, 0), match=''>\n",
      "<re.Match object; span=(0, 0), match=''>\n",
      "<re.Match object; span=(0, 0), match=''>\n"
     ]
    }
   ],
   "source": [
    "regex = \"\"  # write your regex here\n",
    "matches = [  # you should be able to match those\n",
    "    \"202-555-0181\",\n",
    "    \"001 202-555-0181\",\n",
    "    \"+1-512-555-0191\"\n",
    "]\n",
    "non_matches = [  # for now, none of these should be matched\n",
    "    \"202555-0181\",\n",
    "    \"96-125-3546\",\n",
    "    \"(+1)5125550191\"\n",
    "]\n",
    "for s in matches:\n",
    "    print(re.search(regex, s))\n",
    "for s in non_matches:\n",
    "    print(re.search(regex, s))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 分组\n",
    "\n",
    "到目前为止，当我们使用正则表达式来匹配字符串时，我们可以通过在匹配对象上使用 `.group()` 函数来检索匹配的全部信息："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "my nam is\n"
     ]
    }
   ],
   "source": [
    "regex = \"my name? is\"\n",
    "\n",
    "m = re.search(regex, \"my nam is Virgilio\")\n",
    "if m is not None:\n",
    "    print(m.group())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "假设我们再次处理电话号码，我们希望以大文字查找电话号码。但在那之后，我们还希望从数字所在的国家 / 地区提取。我们怎么能这样做..？好吧，我们可以使用正则表达式来匹配电话号码，然后使用第二个正则表达式来提取国家 / 地区代码，对吧？ (我们假设电话号码是按顺序写入数字，没有空格或 \"-\" 将它们分开。)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The country code is: +351\n",
      "The country code is: 001\n",
      "The country code is: +1\n",
      "The country code is: 0048\n"
     ]
    }
   ],
   "source": [
    "regex_number = \"((00|[+])\\d{1,3}[ -])\\d{8,12}\"\n",
    "regex_code = \"((00|[+])\\d{1,3})\"\n",
    "matches = [  # you should be able to match those\n",
    "    \"+351 2025550181\",\n",
    "    \"001 2025550181\",\n",
    "    \"+1-5125550191\",\n",
    "    \"0048 123456789\"\n",
    "]\n",
    "\n",
    "for s in matches:\n",
    "    m = re.search(regex_number, s)  # match the phone number\n",
    "    if m is not None:\n",
    "        phone_number = m.group()    # extract the phone number\n",
    "        code = re.search(regex_code, phone_number)  # match the country code\n",
    "        print(\"The country code is: {}\".format(code.group()))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "但这不仅是重复的，因为我只是将 `regex_number` 的开头复制到 `regex_code` 中，但如果我试图检索我的匹配的几个不同部分，它会变得非常麻烦。因此，正则表达式的功能是 _组_ 。通过对正则表达式的某些部分进行分组，您可以执行诸如使用重复运算符之类的操作，然后 **检索其信息**。\n",
    "\n",
    "要进行分组，只需要使用 `()` 括号。例如，正则表达式 \"(ab)+\" 查找 \"ab\" ， \"abab\" ， \"ababab\" 等形式的匹配。\n",
    "\n",
    "我们还使用分组 [开头](#匹配选项) 来创建一个匹配 \"Virgilio\" 和 \"virgilio\" 的正则表达式，编写 `(V|v)irgilio` 。\n",
    "\n",
    "现在关注真正重要的部分！我们可以使用分组来检索部分匹配，我们使用 `.group()` 函数执行此操作！任何一组 `()` 定义一个组，然后我们可以使用 `.group(i)` 函数来检索组 `i` 。请注意，第 0 组始终是整个匹配，然后从左开始计数！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abc defghi\n",
      "abc defghi\n",
      "abc\n",
      "defghi\n",
      "fg\n",
      "('abc', 'defghi', 'fg')\n"
     ]
    }
   ],
   "source": [
    "regex_with_grouping = \"(abc) (de(fg)hi)\"\n",
    "m = re.search(regex_with_grouping, \"abc defghi jklm n opq\")\n",
    "print(m.group())\n",
    "print(m.group(0))\n",
    "print(m.group(1))\n",
    "print(m.group(2))\n",
    "print(m.group(3))\n",
    "print(m.groups())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请注意，`match.group()`和`match.group(0)`是一回事。还要注意函数`match.groups()`返回元组中的所有组！"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 电话号码 v3\n",
    "\n",
    "使用您目前所学到的知识，编写一个与不同国家 / 地区代码的电话号码相匹配的正则表达式。假设如下：\n",
    "\n",
    "- 国家 / 地区代码以 \"00\" 或 \"+\" 开头，后跟一到三位数字\n",
    "- 电话号码的长度在 8 到 12 之间\n",
    "- 电话号码和国家 / 地区代码用空格 \"\" 或连字符 \"-\" 分隔\n",
    "  \n",
    "让您的代码在我接下来要提供的字符串中查找电话号码，并让它打印出它找到的不同国家 / 地区代码。\n",
    "\n",
    "当正则表达式中包含组时，您可能想要了解 `re.findall()` 的确切行为。你可以通过检查[`re` 模块的文档](https://docs.python.org/3/library/re.html#re.findall)来做到这一点。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "paragraph = \"\"\"Hello, I am Virgilio and I am from Italy.\n",
    "If phones were a thing when I was alive, my number would've probably been 0039 3123456789.\n",
    "I would also love to get a house with 3 floors and something like +1 000 square meters.\n",
    "Now that we are at it, I can also tell you that the number 0039 3135313531 would have suited Leo da Vinci very well...\n",
    "And come to think of it, someone told me that Socrates had dibs on +30-2111112222\"\"\"\n",
    "# you should find 3 phone numbers\n",
    "# and you should not be fooled by the other numbers that show up in the text"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 正则表达式的玩具项目\n",
    "\n",
    "对于玩具项目来说，这远远不是微不足道的，你可以模仿[我在这里做的](http://mathspp.blogspot.com/2017/11/on-computing-all-patterns-matched-by.html )。如果您按照该链接，您将找到一段采用正则表达式的代码，然后打印给定正则表达式匹配的所有字符串。\n",
    "\n",
    "我将给你几个关于它是如何工作的例子："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append(\"./regex-bin\")\n",
    "import regexPrinter\n",
    "\n",
    "def get_iter(regex):\n",
    "    return regexPrinter.printRegex(regex).print()\n",
    "\n",
    "def printall(regex):\n",
    "    for poss_match in get_iter(regex):\n",
    "        print(poss_match)\n",
    "\n",
    "regex = \"V|virgilio\"\n",
    "printall(regex)\n",
    "print(\"-\"*30)\n",
    "regex = \"wo+w\"\n",
    "printall(regex)\n",
    "print(\"-\"*30)\n",
    "# notice that for some reason, dumb me used {n:m} instead of {n,m}\n",
    "# also note that I only implemented {n,m}, and not {n,} nor {,m} nor {n}\n",
    "# also note that this does not support nor \\d nor [0-9]\n",
    "regex = \"((00|[+])1[ -])?[0123456789]{3:3}\"\n",
    "printall(regex)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请注意，代码受到保护以防止无限模式，这些模式用 `...` 发出信号。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "printall(\"this is infinite!+\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果你对这类事情完全不熟悉，那么这看起来完全不可能......但事实并非如此，因为我是一个正常的人，我能够做到！所以如果你真的想要你也可以做到！在链接中，您列出了我决定包含的所有功能，例如排除了 `\\d` 。\n",
    "\n",
    "我只能按照我的方式做到这一点，因为我已经浏览了 [这个惊人的系列](https://ruslanspivak.com/lsbasi-part1/) 中的一些(不是全部)博客文章。\n",
    "\n",
    "也许您可以实现较小的功能子集而不会有太多麻烦？这一点是，如果您知道正则表达式如何工作，则只能打印正则表达式匹配的字符串。尝试从仅实现文字匹配和 `|` 和 `？` 运算符开始。你现在可以包括分组 `()` 以便 `(ab)?` 能按预期工作吗？你能添加 `[]` 吗？那么 `+` 和 `*` 呢？或者也许以 `{n，m}` 开头，分别写 `?`, `+` 和 `*` 作为 `{0,1}` ,  `{1,}` 和 `{0,}` 。\n",
    "\n",
    "你也可以稍微推迟这个项目，并深入挖掘正则表达式的世界。下一节包含一些额外的参考资料和一些练习练习新知识的网站！"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 进一步阅读\n",
    "对于Python中的正则表达式，您可以查看 `re` 模块的 [文档](https://docs.python.org/3/library/re.html) 以及此 [regex HOWTO](https://docs.python.org/3/howto/regex.html) 。\n",
    "\n",
    "一些很好的主题要跟进，包括但不限于：\n",
    "    - 非捕获组 (以及Python的命名组)\n",
    "    - 断言 (先行断言，负面，......)\n",
    "    - 正则表达式编译和标志(用于 Python )\n",
    "    - 递归正则表达式\n",
    "\n",
    "[这个](https://regexr.com/) 有趣的网站(以及 [这一个](https://regex101.com/) 也提供了一个界面供您输入正则表达式并查看它们匹配的内容文本。该工具还可以解释正则表达式的作用。\n",
    "\n",
    "---\n",
    "\n",
    "我找到了一些有关正则表达式练习的有趣网站。 [这一个](https://regexone.com/lesson/introduction_abcs) 有更多的 \"基本\" 练习，每个练习都先解释完成练习所需的一切。我建议你仔细阅读。 [Hackerrank](https://www.hackerrank.com/domains/regex) 和 [regexplay](http://play.inginf.units.it/#/) 也有一些有趣的练习，但那些要求你登录某种程度上来说。\n",
    "\n",
    "---\n",
    "\n",
    "如果您喜欢本指南和/或它很有用，请考虑在 [Virgilio 仓库](https://github.com/clone95/Virgilio) 中 **star** 并与您的朋友分享！\n",
    "\n",
    "这是由 [Mathspp Blog](https://mathspp.blogspot.com)，[RojerGS](https://github.com/RojerGS)带给您的编辑。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 推荐解决方案"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 查找 $\\pi$ (已解决)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pifile = \"regex-bin/pi.txt\"\n",
    "regex = \"9876\"  # define your regex to look your favourite number up\n",
    "\n",
    "with open(pifile, \"r\") as f:\n",
    "    pistr = f.read()  # pistr is a string that contains 1M digits of pi\n",
    "    \n",
    "## search for your number here\n",
    "m = re.search(regex, pistr)\n",
    "if m:\n",
    "    print(\"Found the number '{}' at positions {}\".format(regex, m.span()))\n",
    "else:\n",
    "    print(\"Sorry, the first million digits of pi can't help you with that...\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Virgilio 还是 Virgil？ (已解决)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "paragraphs = \\\n",
    "\"\"\"Publius Vergilius Maro (Classical Latin: [ˈpuː.blɪ.ʊs wɛrˈɡɪ.lɪ.ʊs ˈma.roː]; traditional dates October 15, 70 BC – September 21, 19 BC[1]), usually called virgilio or Vergil (/ˈvɜːrdʒɪl/) in English, was an ancient Roman poet of the Augustan period. He wrote three of the most famous poems in Latin literature: the Eclogues (or Bucolics), the Georgics, and the epic Aeneid. A number of minor poems, collected in the Appendix Vergiliana, are sometimes attributed to him.[2][3]\n",
    "\n",
    "Virgilio is traditionally ranked as one of Rome's greatest poets. His Aeneid has been considered the national epic of ancient Rome since the time of its composition. Modeled after Homer's Iliad and Odyssey, the Aeneid follows the Trojan refugee Aeneas as he struggles to fulfill his destiny and reach Italy, where his descendants Romulus and Remus were to found the city of Rome. virgilio's work has had wide and deep influence on Western literature, most notably Dante's Divine Comedy, in which virgilio appears as Dante's guide through Hell and Purgatory.\"\"\"\n",
    "\n",
    "regex = \"(V|v)irgilio\"\n",
    "parsed_str = paragraphs\n",
    "m = re.search(regex, parsed_str)\n",
    "while m is not None:\n",
    "    parsed_str = parsed_str[:m.start()] + \"Virgil\" + parsed_str[m.end():]\n",
    "    m = re.search(regex, parsed_str)\n",
    "\n",
    "print(parsed_str)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 删除多余的空格 (已解决)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "weird_text = \"Now   it  is your   turn.  I am     going  to give   you this    sentence as        input, and   your  job    is to      fix the     whitespace         in it. When you    are  done,    save the    result in a  string  named   `s`, and   check    if  `s.count(\"  \")` is   equal   to    0  or not.\"\n",
    "regex = \" +\"  # put your regex here\n",
    "# there are several possible solutions, I chose this one\n",
    "\n",
    "# substitute the extra whitespace here\n",
    "s = re.sub(regex, \" \", weird_text)\n",
    "\n",
    "# this print should be 0\n",
    "print(s.count(\"  \"))\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 电话号码 v1 (已解决)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "regex = \"((00|[+])1[ -])?[0-9]{3}-[0-9]{3}-[0-9]{4}\"  # write your regex here\n",
    "matches = [  # you should be able to match those\n",
    "    \"202-555-0181\",\n",
    "    \"001 202-555-0181\",\n",
    "    \"+1-512-555-0191\"\n",
    "]\n",
    "non_matches = [  # for now, none of these should be matched\n",
    "    \"202555-0181\",\n",
    "    \"96-125-3546\",\n",
    "    \"(+1)5125550191\"\n",
    "]\n",
    "for s in matches:\n",
    "    print(re.search(regex, s))\n",
    "for s in non_matches:\n",
    "    print(re.search(regex, s))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `search` 结合 `matched` (已解决)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def my_search(regex, string):\n",
    "    found = False\n",
    "    while string:\n",
    "        m = re.match(regex, string)\n",
    "        if m:\n",
    "            return True\n",
    "        string = string[1:]\n",
    "    # check if the pattern matches the empty string\n",
    "    if re.match(regex, string):\n",
    "        return True\n",
    "    else:\n",
    "        return False\n",
    "\n",
    "regex = \"[0-9]{2,4}\"\n",
    "\n",
    "# your function should be able to match in all these strings\n",
    "string1 = \"1984 was already some years ago.\"\n",
    "print(my_search(regex, string1))\n",
    "string2 = \"There is also a book whose title is '1984', but the story isn't set in the year of 1984.\"\n",
    "print(my_search(regex, string2))\n",
    "string3 = \"Sometimes people write '84 for short.\"\n",
    "print(my_search(regex, string3))\n",
    "\n",
    "# your function should also match with this regex and this string\n",
    "regex = \"a*\"\n",
    "string = \"\"\n",
    "print(my_search(regex, string))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 计数匹配 `findall` (已解决)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def count_matches(regex, string):\n",
    "    return len(re.findall(regex, string))\n",
    "\n",
    "regex = \"wow\"\n",
    "\n",
    "string1 = \"wow wow wow\" # this should be 3\n",
    "print(count_matches(regex, string1))\n",
    "string2 = \"wowow\" # this should be 1\n",
    "print(count_matches(regex, string2))\n",
    "string3 = \"wowowow\" # this should be 2\n",
    "print(count_matches(regex, string3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 电话号码 v2 (已解决)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "regex = \"((00|[+])1[ -])?\\d{3}-\\d{3}-\\d{4}\"  # write your regex here\n",
    "matches = [  # you should be able to match those\n",
    "    \"202-555-0181\",\n",
    "    \"001 202-555-0181\",\n",
    "    \"+1-512-555-0191\"\n",
    "]\n",
    "non_matches = [  # for now, none of these should be matched\n",
    "    \"202555-0181\",\n",
    "    \"96-125-3546\",\n",
    "    \"(+1)5125550191\"\n",
    "]\n",
    "for s in matches:\n",
    "    print(re.search(regex, s))\n",
    "for s in non_matches:\n",
    "    print(re.search(regex, s))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 电话号码 v3 (已解决)\n",
    "\n",
    "对于这个\"问题\"，人们会想到使用`.findall()`函数来查找所有匹配项。当我们这样做时，我们没有获得匹配对象的列表，而是获得带有元组的列表，其中每个元组都有一个来自正则表达式的特定组。这是[记录为`re.findall()`函数]的行为(https://docs.python.org/3/library/re.html#re.findall)。\n",
    "\n",
    "这很好，因为我们真的只关心数字代码，我们可以轻松打印它。如果我们想要匹配对象，那么替代方法是使用[`re.finditer()`](https://docs.python.org/3/library/re.html#re.finditer)函数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "paragraph = \"\"\"Hello, I am Virgilio and I am from Italy.\n",
    "If phones were a thing when I was alive, my number would've probably been 0039 3123456789.\n",
    "I would also love to get a house with 3 floors and something like +1 000 square meters.\n",
    "Now that we are at it, I can also tell you that the number 0039 3135313531 would have suited Leo da Vinci very well...\n",
    "And come to think of it, someone told me that Socrates had dibs on +30-2111112222\"\"\"\n",
    "# you should find 3 phone numbers\n",
    "# and you should not be fooled by the other numbers that show up in the text\n",
    "\n",
    "regex = \"((00|[+])\\d{1,3})[ -]\\d{8,12}\"\n",
    "ns = re.findall(regex, paragraph)  # find numbers\n",
    "for n in ns:\n",
    "    # n is a tuple with the two groups our string has\n",
    "    print(n)\n",
    "    \n",
    "for n in re.finditer(regex, paragraph):\n",
    "    print(\"The number '{}' has country code: {}\".format(n.group(), n.group(1)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
